package com.taotao.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.mapper.TbContentMapper;
import com.taotao.po.TbContent;
import com.taotao.po.TbContentExample;
import com.taotao.po.TbItem;
import com.taotao.po.TbContentExample.Criteria;
import com.taotao.pojo.EasyUIDataGridResult;
import com.taotao.pojo.TaotaoResult;
import com.taotao.service.ContentService;

@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper contentMapper;

	@Override
	public EasyUIDataGridResult getContentListByCatId(Long categoryId,
			int page, int rows) {

		// 设置分页
		PageHelper.startPage(page, rows);

		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		List<TbContent> list = null;
		// 判断category 是否为0
		if (categoryId == 0 || categoryId == null) {
			list = contentMapper.selectByExampleWithBLOBs(example);
		} else {
			criteria.andCategoryIdEqualTo(categoryId);
			list = contentMapper.selectByExampleWithBLOBs(example);
		}
		// 取分页信息
		PageInfo<TbContent> inPageInfo = new PageInfo<TbContent>(list);
		
		// 设置和返回结果
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setTotal(inPageInfo.getTotal());
		result.setRows(list);
		return result;
	}

	@Override
	public TaotaoResult addContent(TbContent content) {
		Date date = new Date();
		content.setCreated(date);
		content.setUpdated(date);
		//插入数据
		contentMapper.insert(content);
		return TaotaoResult.ok();
	}
}
